import 'package:flutter/material.dart';

///加载弹窗
class LoadDialog {
  static dynamic ctx;
  static bool isShow = false;
  static int showNum = 0;
  static Dialog dialog = Dialog(
      child: _DialogWidget(),
      backgroundColor: Colors.transparent,
      elevation: 0);

  static void showLoadDialog({bool isClose = false}) {
    showNum++;
    if (!isShowDialog()) {
      show(isClose: isClose);
    }
  }

  static void cancelLoadDialog() {
    showNum--;
    if (isShowDialog() && showNum <= 0) {
       Navigator.of(ctx).pop();
    }
  }

  static bool isShowDialog() {
    return isShow;
  }

  /// 展示 Widget {isClose  物理键是否可以关闭 弹窗}
  static void show({bool isClose = false}) {
    showDialog(
        context: ctx,
        barrierDismissible: false,
        builder: (BuildContext context) {
          return WillPopScope(
            child: dialog,
            onWillPop: () {
              return Future.value(isClose);
            },
          );
     });
  }
}

//加载弹窗 Widget
class _DialogWidget extends StatefulWidget {
  const _DialogWidget({Key? key}) : super(key: key);

  @override
  __DialogWidgetState createState() => __DialogWidgetState();
}

class __DialogWidgetState extends State<_DialogWidget> {
  @override
  Widget build(BuildContext context) {
    LoadDialog.isShow = true;
    return LinearProgressIndicator(
      backgroundColor: Colors.transparent,
      valueColor: AlwaysStoppedAnimation(Colors.black38),
      color: Colors.transparent,
    );
  }

  @override
  void dispose() {
    super.dispose();
    LoadDialog.isShow = false;
    // LoadDialog.showNum=0;
  }
}
